package com.swas.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swas.entity.Perm;
import com.swas.entity.Role;
import com.swas.entity.RolePerm;
import com.swas.service.RoleService;
import com.swas.util.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public ResultVO selectAll(@RequestParam(defaultValue = "1") Integer pageNum,
           @RequestParam(defaultValue = "8") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleService.selectAll();
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        if (!list.isEmpty()) {
            return ResultVO.success("查询成功", pageInfo);
        } else {
            return ResultVO.fail("查询失败");
        }
    }

    @GetMapping("/selectRole")
    public ResultVO selectSomeByUid(@Param("id") Integer id){
        Integer i = roleService.selectSomeById(id);
        return ResultVO.success("查询成功", i);
    }

    @GetMapping("/updateRoleList/{id},{role}")
    public ResultVO updateRoleList(@PathVariable Integer id, @PathVariable Integer role){
        Integer r = roleService.updateRoleList(id, role);
        return ResultVO.success("修改成功", r);
    }

    @GetMapping("/selectByRoleId")
    public ResultVO selectByRoleId(Integer id){
        List<RolePerm> list = roleService.selectByRoleId(id);
        return ResultVO.success("查询成功", list);
    }

    @GetMapping("/selectByUid")
    public ResultVO selectByUid(Integer id) {
        List<Perm> menuList = roleService.selectByUid(id);
        return ResultVO.success("查询成功", menuList);
    }

    @GetMapping("/selectById")
    public ResultVO selectById(Integer id) {
        List<Perm> perms = roleService.selectById(id);
        return ResultVO.success("查询成功", perms);
    }

    @GetMapping("/updateRole")
    public ResultVO updateRole(Integer id, @RequestParam Integer[] ids){
        boolean flag = roleService.updateRole(id, ids);
        if(flag){
            return ResultVO.success("修改成功");
        }else{
            return ResultVO.fail("修改失败");
        }
    }

}
